package com.shidaimanong.demo.network;

import java.io.*;
import java.net.Socket;

/**
 * 学编程，关注：十代码农
 * 抖音、快手、B站、西瓜视频、今日头条
 */
public class TcpClientDemo {
    public static void main(String[] args) {
        String serverAddress = "localhost";//服务器地址
        int serverPort = 7777;//服务器端口
        try {
            System.out.println("连接到服务器：" + serverAddress + " ，端口号：" + serverPort);
            Socket client = new Socket(serverAddress, serverPort);//连接服务器
            OutputStream outToServer = client.getOutputStream();
            DataOutputStream out = new DataOutputStream(outToServer);
            out.writeUTF("新四有青年来住店。");//发送数据
            InputStream inFromServer = client.getInputStream();
            DataInputStream in = new DataInputStream(inFromServer);
            System.out.println("服务器响应： " + in.readUTF());//接收数据并打印
            client.close();//关闭连接
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}